package com.zcx.project.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zcx.project.entity.ZcxPlanDetail;
import com.zcx.project.mapper.ZcxPlanDetailMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcx.project.entity.ZcxPlanDetailUser;
import com.zcx.project.mapper.ZcxPlanDetailUserMapper;


@Controller
@RequestMapping("/ZcxPlanDetailUser")
public class ZcxPlanDetailUserController {

    @Autowired
    ZcxPlanDetailUserMapper zcxPlanDetailUserMapper;
    @Autowired
    ZcxPlanDetailMapper zcxPlanDetailMapper;


    @RequestMapping(value = "ZcxPlanDetailUserHtml")
    public ModelAndView getMainPage(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("zcxPlanDetailUser");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/findList")
    public List findList(ZcxPlanDetailUser obj) {
        List list = zcxPlanDetailUserMapper.findList(obj);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    public Map<String, Object> findZcxPlanDetailUserPage(int currentPage, int rows, ZcxPlanDetailUser obj) {

        //使用分页插件,核心代码就这一行，页数、每页行数
        PageHelper.startPage(currentPage, rows);
        List<ZcxPlanDetailUser> list = zcxPlanDetailUserMapper.findList(obj);
        PageInfo<ZcxPlanDetailUser> pageInfo = new PageInfo<ZcxPlanDetailUser>(list);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;


    }


    @ResponseBody
    @RequestMapping(value = "/insertZcxPlanDetailUser")
    public Map<String, Object> addZcxPlanDetailUser(ZcxPlanDetailUser obj) {

        ZcxPlanDetail zcxPlanDetail = zcxPlanDetailMapper.selectByPrimaryKey(obj.getPlanDetailId());
        ZcxPlanDetailUser zpu = new ZcxPlanDetailUser();
        zpu.setUserId(obj.getUserId());
        zpu.setPlanStar(zcxPlanDetail.getPlanStar());
        zpu.setPlanEnd(zcxPlanDetail.getPlanEnd());
        ZcxPlanDetailUser verify = zcxPlanDetailUserMapper.verify(zpu);

        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (verify == null) {
            int i = zcxPlanDetailUserMapper.insertSelective(obj);
            if (i == 0)
                returnMap.put("showInfo", "添加失败！");
            else
                returnMap.put("showInfo", "添加成功！");
        } else {
            returnMap.put("showInfo", "添加失败！该人员不能重复排班");
        }


        return returnMap;
    }


    @ResponseBody
    @RequestMapping(value = "/updateZcxPlanDetailUser")
    public Map<String, Object> updateZcxPlanDetailUser(ZcxPlanDetailUser obj) {
        int i = zcxPlanDetailUserMapper.updateByPrimaryKeySelective(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "修改失败！");
        else
            returnMap.put("showInfo", "修改成功！");
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteZcxPlanDetailUser")
    public Map<String, Object> deleteZcxPlanDetailUser(int id) {
        int i = zcxPlanDetailUserMapper.deleteByPrimaryKey(id);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "删除失败！");
        else
            returnMap.put("showInfo", "删除成功！");
        return returnMap;
    }


}
